import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickerDemo extends StatefulWidget {
  @override
  _ImagePickerDemoState createState() => _ImagePickerDemoState();
}

class _ImagePickerDemoState extends State<ImagePickerDemo> {
  File _imageFile;
  dynamic _pickImageError;
  bool isVideo = false;
  String _retrieveDataError;

  void _onImageButtonPressed(ImageSource source) async {
    if (isVideo) {
    } else {
      try {
        _imageFile = await ImagePicker.pickImage(source: source);
        print("path: ${_imageFile.path}");
      } catch (e) {
        _pickImageError = e;
      }
      setState(() {

      });
    }
  }

  void _onVideoControllerUpdate() {
    setState(() {});
  }

  @override
  void deactivate() {
    super.deactivate();
  }

  @override
  void dispose() {
    super.dispose();
  }

  Widget _previewImage() {
    final Text retrieveError = _getRetrieveErrorWidget();
    if (retrieveError != null) {
      return retrieveError;
    }
    if (_imageFile != null) {
      return Image.file(_imageFile);
    } else if (_pickImageError != null) {
      return Text(
        'Pick image error: $_pickImageError',
        textAlign: TextAlign.center,
      );
    } else {
      return const Text(
        'You have not yet picked an image.',
        textAlign: TextAlign.center,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("xxx"),
      ),
      body: Center(
        child: Platform.isAndroid
            ? FutureBuilder<void>(
                builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
//                  switch (snapshot.connectionState) {
//                    case ConnectionState.none:
//                    case ConnectionState.waiting:
//                      return const Text(
//                        'You have not yet picked an image.',
//                        textAlign: TextAlign.center,
//                      );
//                    case ConnectionState.done:
                      return isVideo ? Text("Video") : _previewImage();
//                    default:
//                      if (snapshot.hasError) {
//                        return Text(
//                          'Pick image/video error: ${snapshot.error}}',
//                          textAlign: TextAlign.center,
//                        );
//                      } else {
//                        return const Text(
//                          'You have not yet picked an image.',
//                          textAlign: TextAlign.center,
//                        );
//                      }
//                  }
                },
              )
            : (isVideo ? Text("Video") : _previewImage()),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          FloatingActionButton(
            onPressed: () {
              isVideo = false;
              _onImageButtonPressed(ImageSource.gallery);
            },
            heroTag: 'image0',
            tooltip: 'Pick Image from gallery',
            child: const Icon(Icons.photo_library),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 16.0),
            child: FloatingActionButton(
              onPressed: () {
                isVideo = false;
                _onImageButtonPressed(ImageSource.camera);
              },
              heroTag: 'image1',
              tooltip: 'Take a Photo',
              child: const Icon(Icons.camera_alt),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 16.0),
            child: FloatingActionButton(
              backgroundColor: Colors.red,
              onPressed: () {
                isVideo = true;
                _onImageButtonPressed(ImageSource.gallery);
              },
              heroTag: 'video0',
              tooltip: 'Pick Video from gallery',
              child: const Icon(Icons.video_library),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 16.0),
            child: FloatingActionButton(
              backgroundColor: Colors.red,
              onPressed: () {
                isVideo = true;
                _onImageButtonPressed(ImageSource.camera);
              },
              heroTag: 'video1',
              tooltip: 'Take a Video',
              child: const Icon(Icons.videocam),
            ),
          ),
        ],
      ),
    );
  }

  Text _getRetrieveErrorWidget() {
    if (_retrieveDataError != null) {
      final Text result = Text(_retrieveDataError);
      _retrieveDataError = null;
      return result;
    }
    return null;
  }
}
